/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-07-28 16:17:48
 * @LastEditTime: 2021-08-03 17:10:50
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include <string.h>
#include "ft_debug.h"
#include "dw_i2c.h"
#include "dw_i2c_hw.h"
#include "eeprom.h"
#include "fsleep.h"

#define BUF_LEN     64
static u8 readBuf[BUF_LEN] __attribute__ ((aligned (4)));
static u8 writeBuf[BUF_LEN] __attribute__((aligned(4))) =
    //"test i2c eeprom 2021-8-2 9:39";
    "0123456701234567012345670123456701234567";

#define TEST_READ  1
#define TEST_WRITE 1

void I2cEeMasterWR()
{
    u32 ret = I2C_SUCCESS;
    u32 slaveAddr = AT24C02_ADDR | AT24C02_CHIP_SEL;
    u32 offset = 0x00;

    printf("i2c eeprom read/write test....\r\n");

    memset(readBuf, 0, BUF_LEN);
    ret |= I2cEeMasterInit();

    if (I2C_SUCCESS == ret)
    {
        printf("write buf is \r\n");
        FtDumpHexByte(writeBuf, BUF_LEN);
        ret = I2cEeMasterWrite(slaveAddr, offset, writeBuf, BUF_LEN);
        printf("i2c write test result: 0x%x\r\n", ret);
    }

    fsleep_millisec(2000);

    if (I2C_SUCCESS == ret)
    {
        ret = I2cEeMasterRead(slaveAddr, offset, readBuf, BUF_LEN);
        printf("i2c read test result: 0x%x\r\n", ret);
        printf("read buf is \r\n");
        FtDumpHexByte(readBuf, BUF_LEN);
    }

    printf("test finished result: 0x%x", ret);
    while(1)
    {
        
    }
}

void I2cEeMaster2Slave()
{
    u32 ret = I2C_SUCCESS;
    u32 slaveAddr = 0x0;
    u8 offset = 0x0;

    printf("i2c master/slave read/write test....\r\n");

    memset(readBuf, 0, BUF_LEN);

    ret |= I2cEeMasterInit();
    ret |= I2cEeSlaveInit();

    if (I2C_SUCCESS != ret)
    {
        printf("init failed !!!\r\n");
        return;
    }

    ret |= I2cEeMasterWrite(slaveAddr, offset, writeBuf, 0x16/*sizeof(writeBuf)*/);
    fsleep_millisec(2000);
    I2cEeSlaveReadBuf();
    
    if (I2C_SUCCESS == ret)
    {
        ret = I2cEeMasterRead(slaveAddr, offset, readBuf, 0x16);
        FtDumpHexByte(readBuf, 0x16);
    }
    
    printf("test finished result: 0x%x", ret);  
    fsleep_millisec(1000);  
    while(1)
    {
        fsleep_millisec(10);
    }    
}